<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>

</head>
<body>

<div>

    用户名:<input type="text" name="username" id="userName"><br>
    密码:<input type="text" name="password" id="pwd"><br>
    性别:
    <input type="radio" name="sex" value="0">男
    <input type="radio" name="sex" value="1">女
    <input type="radio" name="sex" value="2">保密
    <br>
    身份：
    <select name="identity" id="sl-identity">
        <option value="0">学生</option>
        <option value="1">老师</option>
        <option value="2">管理员</option>
    </select>
    <br>
    爱好：
    <input type="checkbox" name="hobby" value="0">看书
    <input type="checkbox" name="hobby" value="1">唱歌
    <input type="checkbox" name="hobby" value="2">跳舞
    <br>

    <button id="btn-register">注册</button>
</div>

</body>
</html>

<script src="../static/js/axios.min.js"></script>

<script>

    document.getElementById('btn-register').onclick = function () {
        const userName = document.getElementById('userName').value
        const pwd = document.getElementById('pwd').value
        const identity = document.getElementById('sl-identity').value

        const hobby = []
        const hobbyObj = document.getElementsByName('hobby')
        for (let i = 0; i < hobbyObj.length; i++) {
            if (hobbyObj[i].checked){
                hobby.push(hobbyObj[i].value)
            }
        }
        let sex = document.getElementsByName('sex')
        for (let i = 0; i < sex.length; i++) {
            if (sex[i].checked){
                sex = sex[i].value
            }
        }
        const url = '/user1/register'
        const data = {
            "username":userName,
            "password":pwd,
            sex,
            identity,
            hobby
        }

        axios.post(url, data).then(function (response) {

            console.log(response);

            if (response.data.code === 200) {
                alert('注册成功');
                window.location = '/users/login.html';
            }else {
                alert('注册失败');

            }

        });

        console.log(userName, pwd, identity, hobby, sex)




    }


</script>